package com.rainbow.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainbow.common.util.ResultUtil;
import com.rainbow.common.vo.PageVo;
import com.rainbow.common.vo.Result;
import com.rainbow.modules.sys.entity.Dict;
import com.rainbow.modules.sys.entity.DictData;
import com.rainbow.modules.sys.service.DictDataService;
import com.rainbow.modules.sys.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Auth rainbow
 */
@Slf4j
@RestController
@Api(description = "字典数据管理接口")
@RequestMapping("/rainbow/dictData")
@CacheConfig(cacheNames = "dictData")
@Transactional
public class DictDataController {


    @Autowired
    private DictService dictService;

    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping(value = "/getByCondition")
    @ApiOperation(value = "多条件分页获取用户列表")
    public Result<IPage<DictData>> getByCondition(@ModelAttribute DictData dictData,
                                                  @ModelAttribute PageVo pageVo) {

        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        IPage<DictData> page = dictDataService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), queryWrapper);
        return new ResultUtil<IPage<DictData>>().setData(page);
    }

    @GetMapping(value = "/getByType/{type}")
    @ApiOperation(value = "通过类型获取")
    @Cacheable(key = "#type")
    public Result<Object> getByType(@PathVariable String type) {

        Dict dict = dictService.getOne(new QueryWrapper<Dict>().eq("type", type));
        if (dict == null) {
            return new ResultUtil<Object>().setErrorMsg("字典类型Type不存在");
        }
        List<DictData> list = dictDataService.list(new QueryWrapper<DictData>().eq("dict_id", dict.getId()));
        return new ResultUtil<Object>().setData(list);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加")
    public Result<Object> add(@ModelAttribute DictData dictData) {

        Dict dict = dictService.getById(dictData.getDictId());
        if (dict == null) {
            return new ResultUtil<Object>().setErrorMsg("字典类型id不存在");
        }
        dictDataService.save(dictData);
        // 删除缓存
        redisTemplate.delete("dictData::" + dict.getType());
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "编辑")
    public Result<Object> edit(@ModelAttribute DictData dictData) {

        dictDataService.updateById(dictData);
        // 删除缓存
        Dict dict = dictService.getById(dictData.getDictId());
        redisTemplate.delete("dictData::" + dict.getType());
        return new ResultUtil<Object>().setSuccessMsg("编辑成功");
    }

    @DeleteMapping(value = "/delByIds/{ids}")
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delByIds(@PathVariable String[] ids) {

        for (String id : ids) {
            DictData dictData = dictDataService.getById(id);
            Dict dict = dictService.getById(dictData.getDictId());
            dictDataService.removeById(id);
            // 删除缓存
            redisTemplate.delete("dictData::" + dict.getType());
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
